package com.example.swagger.controller;

import com.example.swagger.entity.Order;
import com.example.swagger.entity.User;
import com.example.swagger.service.OrderService;
import com.example.swagger.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/html")
public class SalesHtmlController {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;


    @ApiOperation(value="hello_html",tags = { "HTML页面" })
    @RequestMapping(value = "/hello")
    public String hello(Model model) {
        model.addAttribute("name", "world");
        return "login";
    }

    @ApiOperation(value="产品列表",tags = { "HTML页面" })
    @RequestMapping("product/list")
    public String showProducts(Model model) {
        List<Order> list = orderService.queryAllOrders();
        model.addAttribute("list",list);
        return "product";
    }

    @ApiOperation(value="产品列表分页",tags = { "HTML页面" })
    @RequestMapping("product/page/{pageNo}/{pageSize}")
    public String showProductPages(Model model, @PathVariable Integer pageNo,@PathVariable Integer pageSize) {
        PageInfo<Order> pageInfo = orderService.getOrdersByPage(pageNo, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "product";
    }

    @ApiOperation(value="产品列表",tags = { "HTML页面" })
    @RequestMapping("/product/add")
    public String addProduct(Order order, MultipartFile imagesFile) {
        System.out.println("------" + imagesFile.getOriginalFilename());
        return "product";
    }

    @ApiOperation(value="用户列表html",tags = { "HTML页面" })
    @RequestMapping(value = "users/list")
    public String showUsers(Model model) {

        List<User> list = userService.queryAllUser();
        model.addAttribute("list",list);
        return "User";
    }

    @ApiOperation(value="用户列表html分页",tags = { "HTML页面" })
    @RequestMapping(value = "users/page/{pageNo}/{pageSize}")
    public String showUsersPage(Model model, @PathVariable Integer pageNo,@PathVariable Integer pageSize) {

        //分页查询，紧跟在其后的第一条查询sql将会被分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<User> pageInfo = userService.queryAllUserByPage(pageNo,pageSize);
        //返回结果
        model.addAttribute("pageInfo",pageInfo);
        return "User";
    }

    @ApiOperation(value="产品列表",tags = { "HTML页面" })
    @RequestMapping("/user/add")
    public String addUser(User user, MultipartFile imagesFile) {
        System.out.println("------" + imagesFile.getOriginalFilename());
        return "User";
    }

    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/login")
    public String loginUser() {
        /**
         * 针对上传文件的处理方式将有变化
         * 两种方式：
         * 1.前台通过表单提交，保存到工程下面的upload目录
         * 路径-->images
         *
         * 2.前台先通过插件异步提交图片
         * 回显到前台---隐藏域---保存图片的地址-----会有垃圾
         *
         * 都要保存到服务器 方式要变化
         * 1.安装FastDFS（耗时最长）-最快的分布式系统
         * 2.引入Java操作FastDFS的一套API
         * 3.获取地址，并将其设置对象
         */
        System.out.println("------" + "来登录");
        return "login";
    }

    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/register")
    public String registerUser() {
        System.out.println("------" + "来注册");
        return "register_more";
    }

    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/game")
    public String showGamelist() {

        return "game";
    }


    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/home")
    public String showHome() {

        return "home";
    }

    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/preference")
    public String showPreference(Model model) {
        model.addAttribute("msg","hello,springboot");
        model.addAttribute("users", Arrays.asList("qianfeng", "gangyao "));
        return "preference";
    }

    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/test")
    public String showTest(Model model) {

       return "test";
    }

    @ApiOperation(value="",tags = { "HTML页面" })
    @RequestMapping("/admin")
    public String showAdmin(Model model) {

        return "../templates/X-admin/login";
    }
}
